﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace He.Framework.AttributeValidate
{
    public static class AttributeValidateExtend
    {
        public static bool Validate(this object oValue)
        {
            Type type = oValue.GetType();
            foreach(var prop in type.GetProperties())
            {
                if (prop.IsDefined(typeof(AbstractValidateAttribute), true))
                {
                    object oPropValue = prop.GetValue(oValue);
                    var attributeArray = prop.GetCustomAttributes<AbstractValidateAttribute>();
                    foreach (var attribute in attributeArray)
                    {
                        if (!attribute.Validate(oPropValue))
                        {
                            return false;
                        }
                    }
                }

                //if (prop.IsDefined(typeof(RequiredAttribute), true))
                //{
                //    object oPropValue = prop.GetValue(oValue);
                //    RequiredAttribute attribute = prop.GetCustomAttribute<RequiredAttribute>();
                //    if (!attribute.Validate(oPropValue))
                //    {
                //        return false;
                //    }
                //}
                //if (prop.IsDefined(typeof(StringLengthAttribute), true))
                //{
                //    object oPropValue = prop.GetValue(oValue);
                //    StringLengthAttribute attribute = prop.GetCustomAttribute<StringLengthAttribute>();
                //    if (!attribute.Validate(oPropValue))
                //    {
                //        return false;
                //    }
                //}

            }
            return true;
        }

    }
}
